//============================================================================
// Name        : rscj.cpp
// Author      : Bob
// Version     :0
// Copyright   : Your copyright notice
// Description : your mom
//============================================================================


#include "stdafx.h"
#include <vector>
#include <ctime>
#include "u_i.h"
#include <boost/lexical_cast.hpp>
#include "fitting.h"
#include "rscj_fit_1.h"

using namespace std;
const int threadnumber=8;
const long double deltai=0.01;
 long double deltat=0.01;
//const long double deltatmax=0.1;
//const long double deltatmin=0.01;
//const long double betac=10;
//const long double omega=2.85;
//const long double imicro=0;
 const double deltab=1;
//u_i uiobj;
void write(vector<long double>& data1,vector<long double>& data2,vector<long double>& data3, int& n);
void write(vector<long double>& data1,vector<long double>& data2, int n,string filename);
void writematrix(vector<long double>& x,vector<long double>& y,vector<vector<long double> >& data);
void writematrix2(vector<long double>& x,vector<long double>& y,vector<vector<long double> >& data);
double benchmark(int threadcount);
double least_squares(vector<long double>& data1,vector<long double>& data2);
void calcdata();
void calcdata2();
void fitt();

int main() {
	
	clock_t begin = clock();

	
	/*vector<double> itemp;
	for(int i=0;i<1001;++i){
		itemp.push_back(-2+0.01*i);
		
	}
	double dIac=0.06;
	for(int i=3;i<50;i+=4){
		std::cout<<i<<std::endl;
	u_i uiobj1=u_i(0.868,1.170051,0.00099972 ,113097335529.23255658465516179806);
	u_i uiobj2=u_i(0.868,1.170051,0.00099972 ,113097335529.23255658465516179806);
	u_i uiobj3=u_i(0.868,1.170051,0.00099972 ,113097335529.23255658465516179806);
	u_i uiobj4=u_i(0.868,1.170051,0.00099972 ,113097335529.23255658465516179806);
	uiobj1.setIac(dIac*i);
	uiobj2.setIac(dIac*(i-1));
	uiobj3.setIac(dIac*(i-2));
	uiobj4.setIac(dIac*(i-3));
	/*uiobj1.setIc2(dIac*0.6795*(i));
	uiobj2.setIc2(dIac*0.6795*(i-1));
	uiobj3.setIc2(dIac*0.6795*(i-2));
	uiobj4.setIc2(dIac*0.6795*(i-3));
	uiobj1.setdphi(-1.57);
	uiobj2.setdphi(-1.57);
	uiobj3.setdphi(-1.57);
	uiobj4.setdphi(-1.57);
	uiobj1.setIex(0.4711);
	uiobj2.setIex(0.4711);
	uiobj3.setIex(0.4711);
	uiobj4.setIex(0.4711);*/
	/*uiobj1.setfilename("microsim5\\"+boost::lexical_cast<std::string>(uiobj1.getIac())+".dat");
	uiobj2.setfilename("microsim5\\"+boost::lexical_cast<std::string>(uiobj2.getIac())+".dat");
	uiobj3.setfilename("microsim5\\"+boost::lexical_cast<std::string>(uiobj3.getIac())+".dat");
	uiobj4.setfilename("microsim5\\"+boost::lexical_cast<std::string>(uiobj4.getIac())+".dat");
	uiobj1.setImin(0);
	uiobj2.setImin(0);
	uiobj3.setImin(0);
	uiobj4.setImin(0);
	uiobj1.setImax(1.5);
	uiobj2.setImax(1.5);
	uiobj3.setImax(1.5);
	uiobj4.setImax(1.5);
	uiobj1.setDeltaI(0.0005);
	uiobj2.setDeltaI(0.0005);
	uiobj3.setDeltaI(0.0005);
	uiobj4.setDeltaI(0.0005);
	/*uiobj1.setDeltaI(itemp);
	uiobj2.setDeltaI(itemp);
	uiobj3.setDeltaI(itemp);
	uiobj4.setDeltaI(itemp);*/
	/*uiobj1.setIasy(0.0);
	uiobj2.setIasy(0.0);
	uiobj3.setIasy(0.0);
	uiobj4.setIasy(0.0);
	//uiobj1.setIex(0.32065);
	//uiobj2.setIex(0.32065);
	//uiobj3.setIex(0.32065);
	//uiobj4.setIex(0.32065);
	//uiobj1.setTemp(0.07);
	//uiobj2.setTemp(0.07);
	//uiobj3.setTemp(0.07);
	//uiobj4.setTemp(0.07);*/
	/*uiobj1.start();
	uiobj2.start();
	uiobj3.start();
	uiobj4.start();
	uiobj1.join();
	uiobj2.join();
	uiobj3.join();
	uiobj4.join();
	uiobj4.write();
	uiobj3.write();
	uiobj2.write();
	uiobj1.write();
	/*for(int i=1;i<100;i=i+4){
		uiobj1.setIac(dIac*i);
		uiobj2.setIac(dIac*(i-1));
		uiobj3.setIac(dIac*(i-2));
		uiobj4.setIac(dIac*(i-3));
		uiobj1.setDeltaI(itemp);
		uiobj2.setDeltaI(itemp);
		uiobj3.setDeltaI(itemp);
		uiobj4.setDeltaI(itemp);
		std::cout<<uiobj1.getIac()<<std::endl;
		std::cout<<uiobj2.getIac()<<std::endl;
		std::cout<<uiobj3.getIac()<<std::endl;
		std::cout<<uiobj4.getIac()<<std::endl;
		uiobj1.setfilename("microsim1\\"+boost::lexical_cast<std::string>(uiobj1.getIac())+".dat");
		uiobj2.setfilename("microsim1\\"+boost::lexical_cast<std::string>(uiobj2.getIac())+".dat");
		uiobj3.setfilename("microsim1\\"+boost::lexical_cast<std::string>(uiobj3.getIac())+".dat");
		uiobj4.setfilename("microsim1\\"+boost::lexical_cast<std::string>(uiobj4.getIac())+".dat");
		uiobj1.start();
		uiobj2.start();
		uiobj3.start();
		uiobj4.start();
		uiobj1.join();
		uiobj2.join();
		uiobj3.join();
		uiobj4.join();
		uiobj1.write();
		uiobj2.write();
		uiobj3.write();
		uiobj4.write();
	}*//*
	uiobj1.~u_i();
	uiobj2.~u_i();
	uiobj3.~u_i();
	uiobj4.~u_i();
	}*/
	


	/*
	double dIac=0.06;
	for(int i=3;i<200;i+=4){
		std::cout<<i<<std::endl;
	u_i uiobj1=u_i(1,1);
	u_i uiobj2=u_i(1,1);
	u_i uiobj3=u_i(1,1);
	u_i uiobj4=u_i(1,1);
	uiobj1.setiac(dIac*i);
	uiobj2.setiac(dIac*(i-1));
	uiobj3.setiac(dIac*(i-2));
	uiobj4.setiac(dIac*(i-3));
	uiobj1.setfilename("microsim4\\"+boost::lexical_cast<std::string>(uiobj1.getiac())+".dat");
	uiobj2.setfilename("microsim4\\"+boost::lexical_cast<std::string>(uiobj2.getiac())+".dat");
	uiobj3.setfilename("microsim4\\"+boost::lexical_cast<std::string>(uiobj3.getiac())+".dat");
	uiobj4.setfilename("microsim4\\"+boost::lexical_cast<std::string>(uiobj4.getiac())+".dat");
	//uiobj.setImin(-1);
	//uiobj.setImax(1);
	/*uiobj1.setDeltaI(itemp);
	uiobj2.setDeltaI(itemp);
	uiobj3.setDeltaI(itemp);
	uiobj4.setDeltaI(itemp);*/
	/*uiobj1.setimin(-4);
	uiobj2.setimin(-4);
	uiobj3.setimin(-4);
	uiobj4.setimin(-4);
	uiobj1.setimax(4);
	uiobj2.setimax(4);
	uiobj3.setimax(4);
	uiobj4.setimax(4);
	uiobj1.start();
	uiobj2.start();
	uiobj3.start();
	uiobj4.start();
	uiobj1.join();
	uiobj2.join();
	uiobj3.join();
	uiobj4.join();
	uiobj4.write();
	uiobj3.write();
	uiobj2.write();
	uiobj1.write();
	/*for(int i=1;i<100;i=i+4){
	//	uiobj1.setIac(dIac*i);
	//	uiobj2.setIac(dIac*(i-1));
	//	uiobj3.setIac(dIac*(i-2));
	//	uiobj4.setIac(dIac*(i-3));
	//	uiobj1.setDeltaI(itemp);
	//	uiobj2.setDeltaI(itemp);
	//	uiobj3.setDeltaI(itemp);
	//	uiobj4.setDeltaI(itemp);
	//	std::cout<<uiobj1.getIac()<<std::endl;
	//	std::cout<<uiobj2.getIac()<<std::endl;
	//	std::cout<<uiobj3.getIac()<<std::endl;
	//	std::cout<<uiobj4.getIac()<<std::endl;
	//	uiobj1.setfilename("microsim1\\"+boost::lexical_cast<std::string>(uiobj1.getIac())+".dat");
	//	uiobj2.setfilename("microsim1\\"+boost::lexical_cast<std::string>(uiobj2.getIac())+".dat");
	//	uiobj3.setfilename("microsim1\\"+boost::lexical_cast<std::string>(uiobj3.getIac())+".dat");
	//	uiobj4.setfilename("microsim1\\"+boost::lexical_cast<std::string>(uiobj4.getIac())+".dat");
	//	uiobj1.start();
	//	uiobj2.start();
	//	uiobj3.start();
	//	uiobj4.start();
	//	uiobj1.join();
	//	uiobj2.join();
	//	uiobj3.join();
	//	uiobj4.join();
	//	uiobj1.write();
	//	uiobj2.write();
	//	uiobj3.write();
	//	uiobj4.write();
	//}*//*
	uiobj1.~u_i();
	uiobj2.~u_i();
	uiobj3.~u_i();
	uiobj4.~u_i();
	}*/
	

	fitt();
	//calcdata();
	//calcdata2();
	//benchmark(4);
	clock_t end = clock();
	double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
	cout<<elapsed_secs<<endl;
	char i;
	cin>>i;
	
//	return elapsed_secs;
}

void fitt(){
	std::string filename;
	rscj_fit_1 modul=rscj_fit_1();
	fitting fitobj=fitting(modul);
	fitobj.setModul(&modul);
	fitobj.set_param_value(0,0.868);	//Rn[Ohm]
	fitobj.set_param_fit(0,false);
	fitobj.set_param_value(1,0.730051);	//Ic[mA]
	fitobj.set_param_fit(1,true);
	fitobj.set_param_value(2,0.0001);//C[nF]
	fitobj.set_param_fit(2,true);
	fitobj.set_param_value(3,0 );	//Iac[mA]
	fitobj.set_param_fit(3,false);
	fitobj.set_param_value(4,1);	//w
	fitobj.set_param_fit(4,false);
	fitobj.set_param_value(5,6.3164e-006);	//gamma
	fitobj.set_param_fit(5,false);
	fitobj.set_param_value(6,0);	//Iasy[mA]
	fitobj.set_param_fit(6,false);
	fitobj.set_param_value(7,0.4);	//Iex[mA]
	fitobj.set_param_fit(7,true);
	fitobj.set_param_value(8,0);	//Ic2[mA]
	fitobj.set_param_fit(8,false);
	fitobj.set_param_value(9,0);	//delta phi
	fitobj.set_param_fit(9,false);
	

	/*filename="1mA/twox925G_4_33K_1mA.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();*/
	
	/*
	filename="micro2/2Ghz_0.001.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="micro2/2Ghz_0.501.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="micro2/2Ghz_1.001.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="micro2/2Ghz_1.501.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="micro2/2Ghz_2.001.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="micro2/2Ghz_2.501.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="micro2/2Ghz_3.001.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="micro2/2Ghz_3.501.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="micro2/2Ghz_4.001.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="micro2/2Ghz_4.501.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="micro2/2Ghz_5.001.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();*/


	/*filename="2mA_pos/twox925G_4_32K_2mA.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();*/
	filename="2mA_pos/twox925G_4_49K_2mA.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="2mA_pos/twox925G_4_89K_2mA.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="2mA_pos/twox925G_5_13K_2mA.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="2mA_pos/twox925G_5_30K_2mA.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="2mA_pos/twox925G_5_35K_2mA.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="2mA_pos/twox925G_5_49K_2mA.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="2mA_pos/twox925G_5_72K_2mA.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="2mA_pos/twox925G_6_05K_2mA.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="2mA_pos/twox925G_6_27K_2mA.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="2mA_pos/twox925G_6_60K_2mA.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="2mA_pos/twox925G_6_94K_2mA.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="2mA_pos/twox925G_7_19K_2mA.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="2mA_pos/twox925G_7_50K_2mA.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();
	filename="2mA_pos/twox925G_7_85K_2mA.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();


/*filename="150uA_joko/twox925A_U-I_4_28K_150uA_T1.dat";
	fitobj.filename=filename;
	fitobj.loadData(filename);
	fitobj.fit();*/
//filename="150uA_joko/twox925A_U-I_4_53K_150uA_T1.dat";
//	fitobj.filename=filename;
//	fitobj.loadData(filename);
//	fitobj.fit();
//filename="150uA_joko/twox925A_U-I_4_65K_150uA_T1.dat";
//	fitobj.filename=filename;
//	fitobj.loadData(filename);
//	fitobj.fit();
//filename="150uA_joko/twox925A_U-I_4_75K_150uA_T1.dat";
//	fitobj.filename=filename;
//	fitobj.loadData(filename);
//	fitobj.fit();
//filename="150uA_joko/twox925A_U-I_4_90K_150uA_T1.dat";
//	fitobj.filename=filename;
//	fitobj.loadData(filename);
//	fitobj.fit();
//filename="150uA_joko/twox925A_U-I_5_05K_150uA_T1.dat";
//	fitobj.filename=filename;
//	fitobj.loadData(filename);
//	fitobj.fit();
//filename="150uA_joko/twox925A_U-I_5_19K_150uA_T1.dat";
//	fitobj.filename=filename;
//	fitobj.loadData(filename);
//	fitobj.fit();
//filename="150uA_joko/twox925A_U-I_5_41K_150uA_T1.dat";
//	fitobj.filename=filename;
//	fitobj.loadData(filename);
//	fitobj.fit();
//filename="150uA_joko/twox925A_U-I_5_60K_150uA_T1.dat";
//	fitobj.filename=filename;
//	fitobj.loadData(filename);
//	fitobj.fit();
//filename="150uA_joko/twox925A_U-I_5_74K_150uA_T1.dat";
//	fitobj.filename=filename;
//	fitobj.loadData(filename);
//	fitobj.fit();
//filename="150uA_joko/twox925A_U-I_6_04K_150uA_T1.dat";
//	fitobj.filename=filename;
//	fitobj.loadData(filename);
//	fitobj.fit();
//filename="150uA_joko/twox925A_U-I_6_20K_150uA_T1.dat";
//	fitobj.filename=filename;
//	fitobj.loadData(filename);
//	fitobj.fit();
//filename="150uA_joko/twox925A_U-I_6_48K_150uA_T1.dat";
//	fitobj.filename=filename;
//	fitobj.loadData(filename);
//	fitobj.fit();
//filename="150uA_joko/twox925A_U-I_6_66K_150uA_T1.dat";
//	fitobj.filename=filename;
//	fitobj.loadData(filename);
//	fitobj.fit();
//filename="150uA_joko/twox925A_U-I_6_87K_150uA_T1.dat";
//	fitobj.filename=filename;
//	fitobj.loadData(filename);
//	fitobj.fit();
//filename="150uA_joko/twox925A_U-I_7_09K_150uA_T1.dat";
//	fitobj.filename=filename;
//	fitobj.loadData(filename);
//	fitobj.fit();
//filename="150uA_joko/twox925A_U-I_7_40K_150uA_T1.dat";
//	fitobj.filename=filename;
//	fitobj.loadData(filename);
//	fitobj.fit();
//filename="150uA_joko/twox925A_U-I_7_61K_150uA_T1.dat";
//	fitobj.filename=filename;
//	fitobj.loadData(filename);
//	fitobj.fit();
//filename="150uA_joko/twox925A_U-I_7_89K_150uA_T1.dat";
//	fitobj.filename=filename;
//	fitobj.loadData(filename);
//	fitobj.fit();
//filename="150uA_joko/twox925A_U-I_8_49K_150uA_T1.dat";
//	fitobj.filename=filename;
//	fitobj.loadData(filename);
//	fitobj.fit();
//filename="150uA_joko/twox925A_U-I_8_73K_150uA_T1.dat";
//	fitobj.filename=filename;
//	fitobj.loadData(filename);
//	fitobj.fit();
	
	//fitobj.fit();


}

double least_squares(vector<long double>& data1,vector<long double>& data2){
long double temp=0;
for(int i=0;i<data1.size();++i){
temp+=pow((data1[i]-data2[i]),2);
}
return temp;
}

double benchmark(int threadcount){
	clock_t begin = clock();
	vector<u_i> uiobj;
	int n,l=0;
	for(int i=0;i<threadcount;++i){
		uiobj.push_back(u_i(1,1));
		uiobj[i].setiac(0);
		uiobj[i].seth(deltat);
		uiobj[i].setimin(0);
		uiobj[i].setimax(5);
		uiobj[i].setDeltai(0.05);
		std::cout<<i<<std::endl;
	}
	for(int i=0;i<threadcount;++i){
		uiobj[i].start();
	}
	n=threadcount-1;
	
	while(n<20){
		for(int i=0;i<threadcount;++i){
			if(uiobj[i].getStatus()==myThread::Stat::FINISHED && n<20){
			++n;
			uiobj[i].setbetac(1);
			uiobj[i].start();
			}
			
		}
		boost::this_thread::sleep(boost::posix_time::milliseconds(100));
	}

	int finished=0;
	int pending=0;
	while(finished<threadcount && pending!=threadcount){
		pending=0;
	for(int i=0; i<threadcount;++i){
		if(uiobj[i].getStatus()==myThread::Stat::FINISHED){
		++finished;
		uiobj[i].setStatus(myThread::Stat::PENDING);
		}else if(uiobj[i].getStatus()==myThread::Stat::PENDING){
			++pending;
		}
	}
	boost::this_thread::sleep(boost::posix_time::milliseconds(100));
	}
	clock_t end = clock();
	double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
	return elapsed_secs;

}

void calcdata(){
vector<long double> betac;
	vector<double> omega;
	vector<double> iac;
	vector<vector<long double> > data;
	vector<long double> current;
	//data.resize(5/deltai);

	vector<u_i> uiobj;
	betac.resize(1);
	betac[0]=deltab;
	omega.resize(1);
	omega[0]=1;
	iac.resize(1);
	iac[0]=0;
	int n,nmax=0;
	nmax=1;

	betac.push_back(0.1);
	betac.push_back(0.75);
	betac.push_back(1);
	betac.push_back(1.5);
	betac.push_back(3);
	betac.push_back(7.5);
	betac.push_back(30);
	betac.push_back(100);
	for(int i=0;i<betac.size();++i){
		omega.push_back(1);
		iac.push_back(0);
	
	}

	//while(betac.back()<5){
	//	//++nmax;
	//	long double temp=betac.back();
	//	betac.push_back(temp+deltab);
	//	omega.push_back(1);
	//	iac.push_back(0);
	//	//cout<<betac.back()<<endl;
	//}
	
	string test;

	for(int i=0;i<threadnumber;++i){
		n=i;
		uiobj.push_back(u_i(betac[i],omega[i]));
		uiobj.at(i).setiac(iac[i]);
		uiobj[i].seth(deltat);
		uiobj[i].setimin(0);
		uiobj[i].setimax(2);
		uiobj[i].setDeltai(0.002);
		uiobj[i].setfilename("data\\"+boost::lexical_cast<std::string>(betac[i])+".txt");
		
		
	}
	for(int i=0;i<threadnumber;++i){
		uiobj[i].start();
	}
	while(n<betac.size()-1){
	for(int i=0; i<threadnumber;++i ){
		if(uiobj[i].getStatus()==myThread::Stat::FINISHED && (n<betac.size()-1)){
		++n;
		data.push_back(uiobj[i].U);
		data.back().push_back(uiobj[i].getbetac());
		uiobj[i].write();
		float status=((((float)n)/((float)betac.size())))*100.0;
		cout<<status<<"%"<<endl;
		uiobj[i].setbetac(betac[n]);
		//cout<<betac[n]<<" "<<uiobj[i].getbetac()<<" "<<uiobj[i].getfilename()<<endl;
		uiobj[i].setfilename("data\\"+boost::lexical_cast<std::string>(betac[n])+".txt");
		
		uiobj[i].start();
		}
	}
	boost::this_thread::sleep(boost::posix_time::milliseconds(100));
	}
	
	int finished=0;
	int pending=0;
	while(finished<threadnumber && pending!=threadnumber){
		pending=0;
	for(int i=0; i<threadnumber;++i){
		if(uiobj[i].getStatus()==myThread::Stat::FINISHED){
		++finished;
		uiobj[i].write();
		data.push_back(uiobj[i].U);
		uiobj[i].setStatus(myThread::Stat::PENDING);
		data.back().push_back(uiobj[i].getbetac());
		}else if(uiobj[i].getStatus()==myThread::Stat::PENDING){
			++pending;
		}
	}
	boost::this_thread::sleep(boost::posix_time::milliseconds(100));
	}


	current=uiobj[0].I;
	vector<long double> betacunsrt;
	for(int i=0;i<data.size();++i){
		betacunsrt.push_back(data[i][data[i].size()-1]);
	}
	writematrix(current,betacunsrt,data);
	
}
void calcdata2(){
vector<long double> smax;
	vector<int> pos;
	vector<long double> smaxtemp;
	vector<double> omega;
	vector<double> iac;
	vector<vector<long double> > data;
	vector<long double> current;
	//data.resize(5/deltai);
	int temp=0;
	vector<long double> betac;
	betac.push_back(0.1);
	vector<vector<long double>> smaxdata;
	//smax.resize(1);
	//smax[0]=50;
	//smax.push_back(100);
	//while(smax.back()<=2000){
	//	//++nmax;
	//	long double temp=smax.at(smax.size()-2);
	//	smax.push_back((temp+100)/2);
	//	smax.push_back(temp+100);
	//	omega.push_back(1);
	//	iac.push_back(0);
	//	//cout<<betac.back()<<endl;
	//}
	smax.push_back(10000);
	for(double temp=50;temp<=2000;temp=temp+50){
		//smax.push_back(temp/2.0);
		smax.push_back(temp);
	}

	while(betac.back()<11){
		data.clear();
	vector<u_i> uiobj;
	
	omega.resize(1);
	omega[0]=1;
	iac.resize(1);
	iac[0]=0;
	int n,nmax=0;
	nmax=1;
	
	data.resize(smax.size());

	string test;
	pos.clear();
	pos.resize(0);
	uiobj.clear();
	uiobj.resize(0);
	for(int i=0;i<threadnumber && i<smax.size();++i){
		n=i;
		uiobj.push_back(u_i(betac.back(),1));
		pos.push_back(i);
		uiobj[i].setiac(0);
		uiobj[i].seth(deltat);
		uiobj[i].setsmax(smax[i]);
		uiobj[i].setimin(0);
		uiobj[i].setimax(5);
		uiobj[i].setfilename("data\\"+boost::lexical_cast<std::string>(smax[i])+".txt");
		
		
	}
	for(int i=0;i<threadnumber && i<smax.size();++i){
		uiobj[i].start();
	}
	while(n<smax.size()-1){
	for(int i=0; i<threadnumber && i<smax.size();++i ){
		if(uiobj[i].getStatus()==myThread::Stat::FINISHED && (n<smax.size()-1)){
			//uiobj[i].write();
		++n;
		temp=0;
		
		data.at(pos.at(i))=(uiobj[i].U);
		//data.back().push_back(uiobj[j].getsmax());
		//uiobj[i].write();
		float status=((((float)n)/((float)smax.size())))*100.0;
		cout<<status<<"%"<<endl;
		uiobj[i].setsmax(smax[n]);
		pos.at(i)=n;
		//cout<<betac[n]<<" "<<uiobj[i].getbetac()<<" "<<uiobj[i].getfilename()<<endl;
		uiobj[i].setfilename("data\\"+boost::lexical_cast<std::string>(smax[n])+".txt");
		
		uiobj[i].start();
		}
	}
	boost::this_thread::sleep(boost::posix_time::milliseconds(100));
	}
	
	int finished=0;
	int pending=0;
	while(finished<uiobj.size() && pending!=threadnumber){
		pending=0;
	for(int i=0; i<threadnumber && i<smax.size();++i){
		if(uiobj[i].getStatus()==myThread::Stat::FINISHED){
			//uiobj[i].write();
		++finished;
		//uiobj[i].write();
		temp=0;
		
		data.at(pos.at(i))=(uiobj[i].U);
		uiobj[i].setStatus(myThread::Stat::PENDING);
		//data.at(temp).push_back(uiobj[i].getbetac());
		}else if(uiobj[i].getStatus()==myThread::Stat::PENDING){
			++pending;
		}
	}
	boost::this_thread::sleep(boost::posix_time::milliseconds(100));
	}


	current=uiobj[0].I;
	//vector<long double> betacunsrt;
	//for(int i=0;i<data.size();++i){
		//betacunsrt.push_back(data[i][data[i].size()-1]);
	//}
	//writematrix(current,smax,data);
	smaxtemp.clear();
	vector<long double> temp;
	for(int i=0;i<smax.size();++i){
		temp.push_back(least_squares(data.at(i),data.at(0)));
		smaxtemp.push_back(smax.at(i));
	}
	smaxdata.push_back(temp);
	betac.push_back(betac.back()+0.1);
	for(int i=0;i<uiobj.size();++i){
		uiobj.at(i).detach();
		uiobj.at(i).~u_i();
	}

	}
	betac.resize(betac.size()-1);
	writematrix(smaxtemp,betac,smaxdata);
	writematrix2(smaxtemp,betac,smaxdata);
	
}

void write(vector<long double>& data1,vector<long double>& data2,vector<long double>& data3, int& n){
	fstream datei;
	datei.open("data.txt", ios::out);
	for(int i=0;i<n;i++){
		datei<<data1[i]<<" "<<data2[i]<<" "<<data3[i]<<endl;
	}
	datei.close();
}

void write(vector<long double>& data1,vector<long double>& data2, int n,string filename){
	fstream datei;
	datei.open(filename, ios::out);
	for(int i=0;i<n;i++){
		datei<<data1[i]<<" "<<data2[i]<<endl;
	}
	datei.close();
}

void writematrix(vector<long double>& x,vector<long double>& y,vector<vector<long double> >& data){
		std::fstream datei;
	datei.open("data.txt", std::ios::out);

	datei<<0.0<<"\t";
		for(int j=0;j<y.size();++j){
		datei<<y[j]<<"\t";
		
		}
		datei<<endl;

		for(int i=0;i<data.at(0).size();++i){
	datei<<x[i]<<"\t";
		for(int j=0;j<data.size();++j){
		datei<<data[j][i]<<"\t";
		
		}
		datei<<endl;
	}
	datei.close();


}
void writematrix2(vector<long double>& x,vector<long double>& y,vector<vector<long double> >& data){
		std::fstream datei;
	datei.open("datamat.txt", std::ios::out);

	for(int i=0;i<y.size() && i<data.size();++i){
		for(int j=0;j<x.size() && j<data.at(i).size();++j){
			datei<<x.at(j)<<" "<<y.at(i)<<" "<<data.at(i).at(j)<<endl;
		}
	}

	datei.close();


}


